package com.lsw.jgyf.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.lsw.Base.AdapterRefresh;
import com.lsw.jgyf.MyApplication;
import com.lsw.jgyf.R;
import com.lsw.jgyf.adapter.SelectionSpecificationsAdapter;
import com.lsw.jgyf.adapter.SelectionSpecificationsColorAdapter;
import com.lsw.jgyf.adapter.SelectionSpecificationsMemoryAdapter;
import com.lsw.jgyf.inject.components.ActivityComponent;
import com.lsw.jgyf.inject.components.DaggerActivityComponent;
import com.lsw.jgyf.inject.modules.ActivityModule;
import com.lsw.jgyf.model.PhoneDetailsModel;
import com.lsw.jgyf.util.GlideUtils;
import com.lsw.util.AppManager;
import com.lsw.util.ToastUtil;
import com.lsw.view.MyGridView;
import com.lsw.view.SelectorNumber;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by Administrator on 2018/7/10 0010.
 */

public class SelectionSpecificationsActivity extends Activity implements SelectionSpecificationsContract.View, AdapterRefresh {

    protected Activity mContext;
    @Inject
    protected SelectionSpecificationsPresenter mPresenter;
    protected Unbinder unbinder;

    protected ActivityComponent mActivityComponent;

    @BindView(R.id.relativeLayout)
    RelativeLayout relativeLayout;
    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;
    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.tv_buy)
    TextView tv_buy;
    @BindView(R.id.gridview1)
    MyGridView gridview1;
    SelectionSpecificationsMemoryAdapter selectionSpecificationsMemoryAdapter;
    @BindView(R.id.gridview2)
    MyGridView gridview2;
    SelectionSpecificationsColorAdapter selectionSpecificationsColorAdapter;
    PhoneDetailsModel model;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_price)
    TextView tv_price;
    @BindView(R.id.selectorNumber)
    SelectorNumber selectorNumber;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selection_specifications);
        mContext = this;
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        AppManager.getAppManager().addActivity(this);
        unbinder = ButterKnife.bind(this);
        mActivityComponent = DaggerActivityComponent.builder().appComponent(MyApplication.getAppComponent())
                .activityModule(new ActivityModule(this)).build();
        initInjector();
        if (mPresenter != null) mPresenter.attachView(this, this);
        initEventAndData();
    }

    public void initInjector() {
        mActivityComponent.inject(this);
    }

    public void initEventAndData() {
        model = (PhoneDetailsModel) getIntent().getSerializableExtra("data");
        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(0, R.anim.bottom_end);
            }
        });
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        selectionSpecificationsMemoryAdapter = new SelectionSpecificationsMemoryAdapter(mContext);
        selectionSpecificationsColorAdapter = new SelectionSpecificationsColorAdapter(mContext);
        selectionSpecificationsColorAdapter.setAdapterRefresh(this);
        gridview1.setAdapter(selectionSpecificationsMemoryAdapter);
        gridview2.setAdapter(selectionSpecificationsColorAdapter);
        if (model != null) {
            selectionSpecificationsMemoryAdapter.setData(model.getMemory());
            if (model.getMemory().size() > 0)
                selectionSpecificationsColorAdapter.setData(model.getMemory().get(0).getColor_list());
            GlideUtils.loadImageView(mContext, model.getPhone().getImg_url(), image);
            tv_name.setText(model.getPhone().getBrand_name() + model.getPhone().getName());
            tv_price.setText(model.getPhone().getPrice());
        }
        gridview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectionSpecificationsMemoryAdapter.setIsseclect(position);
                selectionSpecificationsColorAdapter.setData(selectionSpecificationsMemoryAdapter.getList().get(position).getColor_list());
                if (selectionSpecificationsMemoryAdapter.getList().get(position).getColor_list().size() > 0) {
                    selectionSpecificationsColorAdapter.setIsseclect(0);
                }
            }
        });
        gridview2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectionSpecificationsColorAdapter.setIsseclect(position);
            }
        });
        tv_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectionSpecificationsColorAdapter.getList().size() == 0) {
                    ToastUtil.showToast(mContext, "请选择颜色！");
                    return;
                }
                Intent intent = new Intent(mContext, OrderFillingActivity.class);
                intent.putExtra("memory", selectionSpecificationsMemoryAdapter.getList().get(selectionSpecificationsMemoryAdapter.getIsseclect()).getName());
                intent.putExtra("color", selectionSpecificationsColorAdapter.getList().get(selectionSpecificationsColorAdapter.getIsseclect()).getColor());
                intent.putExtra("brand_name", model.getPhone().getBrand_name());
                intent.putExtra("name", model.getPhone().getName());
                intent.putExtra("price", tv_price.getText().toString().trim());
//                intent.putExtra("price", "0");
                intent.putExtra("number", selectorNumber.getNumber() + "");
                intent.putExtra("id", model.getPhone().getId() + "");
                intent.putExtra("is_contract", model.getPhone().getIs_contract() + "");
                startActivity(intent);
                finish();
                overridePendingTransition(0, R.anim.bottom_end);
            }
        });
    }

    @Override
    public void onRefreshAdapter(int position, int taskid) {
        if (taskid == 1) {
//            tv_price.setText(selectionSpecificationsColorAdapter.getList().get(position).getPrice());
        }
    }

    @Override
    public void setData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.detachView();
        unbinder.unbind();
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // 点击一次返回按钮
            finish();
            overridePendingTransition(0, R.anim.bottom_end);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
